package com.mcp.converter.infrastructure.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * OceanBase数据源配置
 *
 * @author zengxiong
 * @since 2025/05/20 11:08
 */
@Slf4j
@Component
public class OceanBaseClient {
    
    private static final String OCEANBASE_DRIVER_CLASS = "com.oceanbase.jdbc.Driver";
    
    /**
     * 创建OceanBase数据源
     * 
     * @param url OceanBase数据库连接URL
     * @param username 用户名
     * @param password 密码
     * @return DataSource 数据源对象
     * @throws IllegalArgumentException 当参数为空时抛出异常
     */
    public static DataSource createDataSource(String url, String username, String password) {
        // 参数验证
        if (!StringUtils.hasText(url) || !StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            throw new IllegalArgumentException("OceanBase连接参数不能为空: url=" + url + ", username=" + username);
        }
        
        log.info("创建OceanBase数据源, URL: {}, 用户名: {}", url, username);
        
        try {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(OCEANBASE_DRIVER_CLASS);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            
            // 设置OceanBase连接属性
            Properties connectionProperties = getProperties();

            dataSource.setConnectionProperties(connectionProperties);
            
            log.info("OceanBase数据源创建成功");
            return dataSource;
            
        } catch (Exception e) {
            log.error("创建OceanBase数据源失败: {}", e.getMessage(), e);
            throw new RuntimeException("创建OceanBase数据源失败", e);
        }
    }

    private static Properties getProperties() {
        Properties connectionProperties = new Properties();
        // 自动重连
        connectionProperties.setProperty("autoReconnect", "true");
        // Socket超时时间 (30分钟) - 增加超时时间解决超时问题
        connectionProperties.setProperty("socketTimeout", "1800000");
        // 连接超时时间 (5分钟) - 增加连接超时时间
        connectionProperties.setProperty("connectTimeout", "300000");
        // 使用本地会话状态
        connectionProperties.setProperty("useLocalSessionState", "true");
        // 字符编码设置
        connectionProperties.setProperty("useUnicode", "true");
        connectionProperties.setProperty("characterEncoding", "utf-8");
        // 性能优化设置 - 启用批量重写提高性能
        connectionProperties.setProperty("rewriteBatchedStatements", "true");
        connectionProperties.setProperty("cachePrepStmts", "true");
        connectionProperties.setProperty("prepStmtCacheSize", "500"); // 增加缓存大小
        connectionProperties.setProperty("prepStmtCacheSqlLimit", "4096"); // 增加SQL缓存限制
        // 连接池相关设置
        connectionProperties.setProperty("useServerPrepStmts", "true");
        connectionProperties.setProperty("maintainTimeStats", "false");
        // 增加网络缓冲区大小提高性能
        connectionProperties.setProperty("netBufferLength", "32768");
        // 启用压缩传输
        connectionProperties.setProperty("useCompression", "true");
        // 设置最大允许的数据包大小
        connectionProperties.setProperty("maxAllowedPacket", "16777216");
        return connectionProperties;
    }
}
